#include "CRocketLauncherBullet.h"
#include "..\CCyberSpaceState.h"
#include "..\COptionsState.h"
#include "..\CGame.h"
#include "CPlayer.h"

CRocketLauncherBullet::CRocketLauncherBullet(void)
{
	SetWidth(32);
	SetHeight(64);

	m_nShockwaveID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_Shockwave.png");
	m_nExplodeID = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/SGP_RocketHit.wav");
	m_nBulletID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_RocketBullet.png");

	// Set sound volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nExplodeID, COptionsState::GetInstance()->GetSFXVolume());
	m_fShockwaveScale = 1.0f;
	m_bExplode = false;
	m_fRcktRotation = 0.0f;
	m_tRcktVec.fX = 0.0f;
	m_tRcktVec.fY = -1.0f;

	CSGP_EventSystem::GetInstance()->RegisterClient("destroy.all.bullets", this);
}

CRocketLauncherBullet::~CRocketLauncherBullet(void)
{
	CSGP_EventSystem::GetInstance()->UnregisterClient("destroy.all.bullets", this);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBulletID);
}

void CRocketLauncherBullet::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	tVector2D hard;
	hard.fX = 0;
	hard.fY = 1;
	tVector2D it;
	tVector2D player;
	player.fX = GetPosX();
	player.fY = GetPosY();
	tVector2D turret;
	turret.fX = CPlayer::GetInstance()->GetMouseVector().fX;
	turret.fY = CPlayer::GetInstance()->GetMouseVector().fY;
	it.fX = player.fX - turret.fX;
	it.fY = player.fY - turret.fY;

	m_fRcktRotation = AngleBetweenVectors(it, hard);
	if(CPlayer::GetInstance()->GetMouseVector().fX <= GetPosX())
	{
		m_fRcktRotation = -m_fRcktRotation;
	}



	// Update the rotation vector
	//m_fRcktRotation = AngleBetweenVectors(CPlayer::GetInstance()->GetMouseVector(), m_tRcktVec);

	tVector2D tTemp;
	tTemp.fX = CPlayer::GetInstance()->GetMouseVector().fX - GetPosX();
	tTemp.fY = CPlayer::GetInstance()->GetMouseVector().fY - GetPosY();

	float distance = sqrt( (tTemp.fX * tTemp.fX) + (tTemp.fY * tTemp.fY));
	tTemp = Vector2DNormalize(tTemp);

	SetVelX(tTemp.fX*200);
	SetVelY(tTemp.fY*200);

	
	if(distance < 15)
	{
		m_bExplode = true;
	}

	

	if(m_bExplode)
	{
		
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nExplodeID);
			CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyRocketBulletMessage(this));
	}
}

void CRocketLauncherBullet::Render(void)
{

	RECT drawRect = GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();

	m_rRocketRect = GetCollisionRect();
	m_rRocketRect.top -= CCamera::GetInstance()->GetOffsetY();
	m_rRocketRect.left -= CCamera::GetInstance()->GetOffsetX();
	m_rRocketRect.right -= CCamera::GetInstance()->GetOffsetX();
	m_rRocketRect.bottom -= CCamera::GetInstance()->GetOffsetY();

	CSGD_TextureManager::GetInstance()->Draw(m_nBulletID, (int)drawRect.left, (int)drawRect.top, 1.0f, 1.0f, NULL, (GetWidth()*0.5f), (GetHeight()*0.5f),
		m_fRcktRotation);

	// Draw collision box
	CSGD_Direct3D::GetInstance()->DrawRect(drawRect, 0, 255, 0);

	// Draw shockwave
	if(m_bExplode)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nShockwaveID, (int)GetDetonateRect().left - (int)(32*m_fShockwaveScale), 
			(int)GetDetonateRect().top - (int)(32*m_fShockwaveScale), 
			m_fShockwaveScale, m_fShockwaveScale);
	}

	CSGD_Direct3D::GetInstance()->DrawRect(m_rDetonate, 255, 0, 0);
}

RECT CRocketLauncherBullet::GetCollisionRect(void)
{
	/*RECT rIntersect;
	rIntersect.left = long(GetPosX() - CCamera::GetInstance()->GetOffsetX());
	rIntersect.top = long(GetPosY() - CCamera::GetInstance()->GetOffsetY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;*/

	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

bool CRocketLauncherBullet::CheckCollision(IBaseInterface* pBase)
{

	return false;
}

void CRocketLauncherBullet::HandleEvent(CEvent* pEvnt)
{
	if(pEvnt->GetEventID() == "destroy.all.bullets")
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyRocketBulletMessage(this));
	}
}

RECT CRocketLauncherBullet::GetDetonateRect()
{
	RECT rect;
	rect.top = LONG(GetPosY() - GetWidth()/2);
	rect.left = LONG(GetPosX() - GetHeight()/2);
	rect.right = LONG(rect.left + GetWidth() * 6);
	rect.bottom = LONG(rect.top + GetHeight() * 6);
	return rect;
}
